import { createStore } from 'vuex'
import { getLogin, user } from '../utils/api'
import vuexp from 'vuex-persistedstate'

export default createStore({
  plugins: [vuexp()],
  state: {
    token: localStorage.getItem('vuex') || '',
    email: '',
    name: '',
    id: '',
    column: ''
  },
  getters: {
  },
  mutations: {
    setToken(state, data) {
      state.token = data.token
    },
    setName(state, obj) {
      console.log(20, obj);
      state.name = obj.nickName
      state.email = obj.email
      state.id = obj._id
      state.column = obj.column
    }
  },
  actions: {
    async login({ commit }, data) {
      console.log(19, data);
      let relute = await getLogin(data)
      console.log(15, relute);
      commit('setToken', relute.data)
      if (relute.data.token) {
        user().then(res => {
          console.log(25, res);
          commit('setName', res.data)
        })
      }
    }
  },
  modules: {
  }
})
